<%= include ic/admin-header.html %>

<div class="am-cf admin-main">
  <%= include ic/admin-sidebar.html %>

  <!-- content start -->
  <div class="admin-content">
    <div class="am-cf am-padding">
      <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">新闻管理</strong> / <small><% if(result){ %>编辑 - <%= result.get("title") %><% }else{ %>新建<% } %></small></div>
    </div>

	<div class="am-padding-left"><% if(result){ %><%= result.get("title") %><% }else{ %>Create News<% } %></div>
    <hr style=" margin-top:0.5rem;" />
	<div class="am-g-fixed am-cf am-padding-left am-padding-bottom">
		<form class="am-form am-u-sm-12 am-form-horizontal am-cf" id="my-form">
			<% if(result){ %><input id="data-id" type="hidden" value="<%= result.id %>"><% } %>
			<div id="coverBox" class="am-u-sm-4 am-fl">
				<input id="data-cover" type="hidden" value="<% if(result && result.get('cover')){ %><%= result.get('cover') %><% } %>" >
				<div class="am-u-sm-12 am-nbfc">
					<img class="am-u-sm-12" id="imgCover" src="<% if(result && result.get('cover')){ %>/admin/p?id=<%= result.get('cover') %><% }else{ %>/i/default.png<% } %>" />
				</div>
				<div class="am-u-sm-12 am-cf"></div>
				<div class="am-form-group am-form-file am-u-sm-10 am-u-sm-centered am-padding-top">
					<button type="button" class="am-btn am-btn-danger am-btn-sm am-u-sm-centered" id="btn-uploadCover">
						<i class="am-icon-cloud-upload"></i> 选择要上传的文件</button>
					<input id="doc-form-file" type="file" accept="image/*" >
				</div>
			</div>
			<div class="am-u-sm-8 am-fr">
				<div class="am-form-group">
					<label for="data-title" class="am-u-sm-2 am-form-label">标题</label>
					<div class="am-u-sm-10">
						<input type="text" id="data-title" value="<% if(result){ %><%= result.get("title") %><% } %>" placeholder="2-70字符" data-foolish-msg="2-20字符的页面名" minlength="2" maxlength="70" required>
					</div>
				</div>

				<div class="am-form-group am-form-inline">
					<label for="data-author" class="am-u-sm-2 am-form-label">作者</label>
					<div class="am-u-sm-3">
            <input id="data-user" type="hidden" value="<% if(result && result.get("user")){ %><%= result.get("user").id %><% }else{ %><%= user.id %><% } %>">
            <input type="text" id="data-author" value="<% if(result && result.get("user")){ %><%= result.get("user").get("displayName") %><% }else{ %><%= user.get("displayName") %><% } %>" disabled>
					</div>
					<div class="am-u-sm-2"></div>
				</div>
        <div class="am-form-group am-form-inline">
					<label for="data-likes" class="am-u-sm-2 am-form-label">收藏</label>
					<div class="am-u-sm-3">
            <input type="text" id="data-likes" value="<% if(result){ %><%= result.get("likes") %><% }else{ %>0<% } %>" disabled>
					</div>
          <label for="data-clicks" class="am-u-sm-2 am-form-label">阅读</label>
					<div class="am-u-sm-3">
            <input type="text" id="data-clicks" value="<% if(result){ %><%= result.get("clicks") %><% }else{ %>0<% } %>" disabled>
					</div>
          <div class="am-u-sm-2"></div>
				</div>
        <div class="am-form-group am-form-inline">
          <label for="data-state" class="am-u-sm-2 am-form-label">状态</label>
					<div class="am-u-sm-3">
            <input type="text" id="data-state" value="<% if(result && result.get("enabled")){ %>已发布<% }else{ %>草稿<% } %>" disabled>
					</div>
          <label for="data-createdAt" class="am-u-sm-2 am-form-label">创建</label>
					<div class="am-u-sm-5">
            <input type="text" id="data-createdAt" value="<% if(result){ %><%= moment(result.createdAt).format('YYYY年MM月DD日 HH:mm:ss') %><% }else{ %><%= moment().format('YYYY年MM月DD日 HH:mm:ss') %><% } %>" disabled>
					</div>
				</div>
			</div>
		</form>
		<hr class="am-u-sm-12 am-fl am-cf" />
		<div class="am-u-sm-12 am-fl">
			<link rel="stylesheet" type="text/css" href="/css/simditor.css">
			<link rel="stylesheet" type="text/css" href="/css/simditor-html.css">
			<textarea id="data-content" class="am-pre-scrollable" style="height:40rem;max-height:40rem;"><% if(result && result.get("content")){ %><%= result.get("content") %><% } %></textarea>
		</div>
		<hr class="am-u-sm-12 am-fl am-cf" />
		<div class="am-g am-u-sm-11 am-fl am-cf">
			<div class="am-u-sm-offset-1 am-u-sm-3">
				<div class="am-btn am-btn-default" onclick="history.go(-1)">返回列表</div>
			</div>
      <div class="am-u-sm-offset-1 am-u-sm-3">
				<div id="btn-saveEdit0" class="am-btn am-btn-primary btn-saveEdit" onclick="saveEdit(0)">存为草稿</div>
			</div>
			<div class="am-u-sm-offset-1 am-u-sm-3 am-u-end">
				<div id="btn-saveEdit1" class="am-btn am-btn-primary btn-saveEdit" onclick="saveEdit(1)">保存发布</div>
			</div>
		</div>
		<div class="am-u-sm-12 am-fl am-cf" ></div>
	</div>
  </div>
  <!-- content end -->

</div>

<a class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu" data-am-offcanvas="{target: '#admin-offcanvas'}"></a>

<%= include ic/admin-footer.html %>
<script type="text/javascript" src="/js/simditor/module.min.js"></script>
<script type="text/javascript" src="/js/simditor/hotkeys.min.js"></script>
<script type="text/javascript" src="/js/simditor/uploader.min.js"></script>
<script type="text/javascript" src="/js/simditor/simditor.min.js"></script>
<script type="text/javascript" src="/js/simditor/beautify-html.js"></script>
<script type="text/javascript" src="/js/simditor/simditor-html.js"></script>

<script type="text/javascript">
var refer	= "list";
var editor, toolbar,enabled=0;
$(function() {
    toolbar = ['title', 'bold', 'italic', 'underline', 'color', '|', 'ol', 'ul', 'blockquote', 'table', '|', 'link', 'image', 'hr', '|', 'indent', 'outdent', 'alignment'];
    editor = new Simditor({
      textarea: $('#data-content'),
      placeholder: '这里输入文字...',
      toolbar: toolbar,
      pasteImage: true,
      defaultImage: '/i/default.png',
      upload: {
		url: '/admin/uploadImg',
		fileKey: 'uploadImg',
		connectionCount: 3,
		leaveConfirm: '正在上传图片..'
	  }
    });
});
$(function(){
	$("#collapse-nav").addClass("am-in");
	$("#doc-form-file").on("change",function(e){
		uploadImage(this.files[0]);
	});
})
var validator 	= $('#my-form').validator({submit: function(e) {
	if(this.isFormValid()){
		//$.AMUI.progress.start();
		$.post("./save", {
			"ajax"			: true,
			"id"			: $('#data-id').val(),
			"content"		: editor.getValue(),
			"title"			: $('#data-title').val(),
			"user"		  : $('#data-user').val(),
			"cover"			: $('#data-cover').val(),
			"enabled"		: enabled
		},function(data){
			//$.AMUI.progress.done();
      //console.dir(data);
			$(".btn-saveEdit").removeClass("am-disabled");
			if(typeof(data)=="object" && typeof(data.error)=="undefined"){
				mAlert({
					title:"提示",
					text:"保存成功！",
					onConfirm:function(e) {
						gotoUrl(refer);
					}
				});
			}else{
				//console.dir(data);
				mAlert({
					title:"错误",
					text:data.error,
					onConfirm:function(e) {}
				});
			}
		}, "json");
	}else{
		$("#btn-saveEdit").removeClass("am-disabled");
	}
	return false;
}});

function saveEdit(state){
  if(state==1)enabled=state;
	$(".btn-saveEdit").addClass("am-disabled");
	validator.submit();
}
</script>
